<%
Class FastAsp_Trace
    Private m_Enabled    '是否启用跟踪
    Private m_Points     '跟踪点列表
    Private m_StartTime  '开始时间
    Private m_LastTime   '上次时间
    
    Private Sub Class_Initialize()
        m_Enabled = GetConfig("DebugMode")
        Set m_Points = Server.CreateObject("Scripting.Dictionary")
        m_StartTime = Timer()
        m_LastTime = m_StartTime
    End Sub
    
    '添加跟踪点
    Public Function Point(byval name)
        If Not m_Enabled Then Exit Function
        
        Dim now_ : now_ = Timer()
        Dim pointData : Set pointData = Server.CreateObject("Scripting.Dictionary")
        pointData.Add "name", name
        pointData.Add "time", now_
        pointData.Add "runtime", FormatNumber(now_ - m_StartTime, 4)
        pointData.Add "step", FormatNumber(now_ - m_LastTime, 4)
        pointData.Add "memory", GetMemoryUsage()
        
        m_Points.Add m_Points.Count, pointData
        m_LastTime = now_
        Set Point = Me
    End Function
    
    '显示跟踪信息
    Public Sub Show()
        If Not m_Enabled Then Exit Sub
        
        Response.Write "<style type='text/css'>"
        Response.Write ".trace-panel{margin:10px;padding:10px;border:1px solid #ccc;font-size:12px;}"
        Response.Write ".trace-panel table{width:100%;border-collapse:collapse;}"
        Response.Write ".trace-panel th,.trace-panel td{padding:5px;border:1px solid #ddd;text-align:left;}"
        Response.Write ".trace-panel th{background:#f5f5f5;}"
        Response.Write "</style>"
        
        Response.Write "<div class='trace-panel'>"
        Response.Write "<table>"
        Response.Write "<tr>"
        Response.Write "<th>跟踪点</th>"
        Response.Write "<th>总耗时(s)</th>"
        Response.Write "<th>步耗时(s)</th>"
        Response.Write "<th>内存(MB)</th>"
        Response.Write "</tr>"
        
        Dim key, pointData
        For Each key In m_Points.Keys
            Set pointData = m_Points(key)
            Response.Write "<tr>"
            Response.Write "<td>" & Server.HTMLEncode(pointData("name")) & "</td>"
            Response.Write "<td>" & pointData("runtime") & "</td>"
            Response.Write "<td>" & pointData("step") & "</td>"
            Response.Write "<td>" & FormatNumber(pointData("memory")/1024/1024, 2) & "</td>"
            Response.Write "</tr>"
        Next
        Response.Write "</table>"
        Response.Write "</div>"
    End Sub
    
    '获取内存使用
    Private Function GetMemoryUsage()
        On Error Resume Next
        Dim mem : Set mem = GetObject("winmgmts:root\cimv2")
        Dim procs : Set procs = mem.ExecQuery("Select * from Win32_Process Where ProcessId = " & Request.ServerVariables("PROCESS_ID"))
        Dim proc
        For Each proc In procs
            GetMemoryUsage = proc.WorkingSetSize
            Exit Function
        Next
        GetMemoryUsage = 0
    End Function
    
    Private Sub Class_Terminate()
        Set m_Points = Nothing
    End Sub
End Class
%> 